package com.sys.orders.back.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.users.pojo.AAdmins;
import com.sys.users.service.IUsersDbService;

/**
 * 管理员首页操作
 * 
 * @author
 */
@Controller
@RequestMapping(value = "/back/admins", produces = "text/html;charset=UTF-8")
public class AdminsBackController extends BaseController
{
	@Autowired
	private IUsersDbService usersDbService;

	/**
	 * 首页
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/main")
	public String main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("----main---");

		return "/back/admins/main";
	}

	/**
	 * 欢迎页面
	 * 
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping("/welcome")
	public String welcome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--welcome--");
		return "/back/admins/welcome";
	}

	/**
	 * 退出页面
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--logout--");
		HttpSession session = request.getSession();
//		String returnUrl = request.getParameter("returnUrl");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 获取用户 */
		AAdmins admins = (AAdmins) this.findObjfromSession(request, "admins");
//		if (returnUrl == null)
//		{
//			returnUrl = "";
//		}
		try
		{
			/* 做一些清除的操作 */
			session.removeAttribute("admins");
			session.removeAttribute("urlList");
			session.removeAttribute("ipNow");
//			returnUrl = URLEncoder.encode(returnUrl, "UTF-8");
			apiResponse.setCode(ApiResponseEnum.INFO_LOGOUT.getStatus());
		} catch (Exception e)
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		/* 提示信息 */
		session.setAttribute("response", apiResponse.toJSON());
		/* 客户端跳转到登陆页面 */
//		response.sendRedirect(request.getContextPath() + "/login.htm?returnUrl=" + returnUrl);
		return "redirect:/adminsLogin";
	}
	/*==管理员开始==*/
	/**
	 * 管理员列表页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<AAdmins> apiResponse = this.usersDbService.findCondListAdminsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		return "/back/admins/adminsList" ;
	}
	/**
	 * 打开添加管理员页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/adminsInsert")
	public String adminsInsert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminInsert--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		return "/back/admins/adminsInsert" ;
	}
	
	/**
	 * 添加管理员提交操作
	 * 
	 * @return
	 */
	@RequestMapping("/adminsInsertSubmit")
	@ResponseBody
	public String adminsInsertSubmit(HttpServletRequest request,HttpServletResponse response,
			AAdmins admins)throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsInsertSubmit--");
		/* 保存结果 */
		ApiResponse dbResponse = this.usersDbService.saveOneAdminsService(admins);
		ConstatFinalUtil.SYS_LOGGER.info("--用户添加返回结果:{}--", dbResponse.toJSON());
		String result =  dbResponse.toJSON().toJSONString();
		return result;	
	}
	/**
	 * 打开更新管理员页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/adminsUpdate")
	public String adminsUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		// operType=update&idType=session
		ConstatFinalUtil.SYS_LOGGER.info("--adminsUpdate--");
		String adminId = request.getParameter("adminId");
		String operType = request.getParameter("operType");
		
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("adminId", adminId);
		/* 查询数据库 */
		ApiResponse<AAdmins> apiResponse = this.usersDbService.findOneAdminsService(condMap);
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		
		if ("update".equalsIgnoreCase(operType))
		{
			return "/back/admins/adminsUpdate";
		} else if ("updatePass".equalsIgnoreCase(operType))
		{
			/* 更新密码 */
			return "/back/admins/adminsUpdatePass";
		} else if ("resetPass".equalsIgnoreCase(operType))
		{
			/* 重置密码 */
			return "/back/admins/adminsResetPass";
		}
		return "/back/admins/adminsInfo";
	}
	/**
	 * 更新管理员提交操作
	 * 
	 * @return
	 */
	@RequestMapping("/adminsUpdateSubmit")
	@ResponseBody
	public String adminsUpdateSubmit(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--adminsUpdateSubmit--");
		/* 数据库操作 */
		HttpSession session = request.getSession();
		ApiResponse dbResponse = new ApiResponse<>();
		String adminId = request.getParameter("adminId");
		String operType = request.getParameter("operType");

		String idType = request.getParameter("idType");
		if (idType == null)
		{
			idType = "";
		}
		if (!"".equalsIgnoreCase(idType))
		{
			AAdmins adminsSess = (AAdmins) this.findObjfromSession(request, "admins");
			adminId = adminsSess.getAdminId() + "";
		}

		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("adminId", adminId);
		/* 查询数据库 */
		ApiResponse<AAdmins> apiResponse = this.usersDbService.findOneAdminsService(condMap);
		/* 获取java对象 */
		AAdmins admins = apiResponse.getDataOneJava();
		boolean flag = false;
		if ("update".equalsIgnoreCase(operType))
		{
			/* 接收参数 */
			String adminName = request.getParameter("adminName");
			String adminPassword = request.getParameter("adminPassword");
			String loginName = request.getParameter("loginName");
			String status = request.getParameter("status");
			admins.setAdminName(adminName);
			admins.setAdminPassword(adminPassword);
			admins.setLoginName(loginName);
			admins.setStatus(Byte.valueOf(status));
			flag = true;
		} else if ("updatePass".equalsIgnoreCase(operType))
		{
			/* 更新密码 */
			String adminPassword = request.getParameter("adminPassword");
			String souPass = request.getParameter("souPass");
			if (this.encryptUtil.checkStr(souPass, admins.getAdminPassword()))
			{
				/* 原密码正确 */
				admins.setAdminPassword(this.encryptUtil.encodeStr(adminPassword));
				flag = true;
			} else
			{
				/* 原密码不正确 */
				dbResponse.setCode(ApiResponseEnum.INFO_SOUPASS_INCORRECT.getStatus());
			}
		} else if ("resetPass".equalsIgnoreCase(operType))
		{
			String adminPassword = request.getParameter("adminPassword");
			/* 重置密码 */
			admins.setAdminPassword(this.encryptUtil.encodeStr(adminPassword));
			flag = true;
		}
		dbResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(dbResponse.getCode() + ""), Collections.EMPTY_MAP);
		if (flag)
		{
			dbResponse = usersDbService.updateOneAdminsService(admins);
		}
		ConstatFinalUtil.SYS_LOGGER.info("--用户更新返回结果:{}--", dbResponse.toJSON());
		String result =  dbResponse.toJSON().toJSONString();
		return result;
	}
	
}
